package com.cbh.domain.service;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cbh.common.id.IdWorker;
import com.cbh.entity.Payway;
import com.cbh.mapper.PaywayMapper;

@Service
public class PaywayService {
	private static final Log logger = LogFactory.getLog(PaywayService.class);
	@Autowired
	PaywayMapper payMapper;

	/**
	 * 保存支付方式信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String save(Payway ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		if (StringUtils.isEmpty(ob.getPmId())) {
			return insert(ob);
		}
		return update(ob);
	}

	/**
	 * 保存支付方式信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String insert(Payway ob) throws Exception {
		if (null == ob) {
			throw new Exception("数据对象为空");
		}
		ob.setPmId(IdWorker.getId().toString());

		if (null == ob.getPmName()) {
			throw new Exception("请传入名称");
		}
		if (null == ob.getPmLogo()) {
			throw new Exception("请传入Logo");
		}
		if (null == ob.getPmCode()) {
			throw new Exception("请传入Code");
		}

		try {
			Integer re = payMapper.insert(ob);
			if (re > 0) {
				return ob.getPmId();
			} else {
				throw new Exception("保存数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("保存数据失败");
		}
	}

	/**
	 * 保存支付方式信息
	 * 
	 * @param pro
	 * @return
	 * @throws Exception
	 */
	public String update(Payway ob) throws Exception {
		try {
			Integer re = payMapper.update(ob);
			if (re > 0) {
				return ob.getPmId();
			} else {
				throw new Exception("更新数据失败");
			}
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("更新数据失败");
		}
	}

	/**
	 * 真删除支付方式
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public boolean realDelete(String id) throws Exception {
		Payway ob = new Payway().id(id);
		try {
			Integer re = payMapper.delect(ob);
			return re == 1;
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("删除数据失败");
		}
	}

	/**
	 * 根据proId获取支付方式信息
	 * 
	 * @param proId
	 * @return
	 * @throws Exception
	 */
	public Payway getOne(String id) throws Exception {
		if (null == id) {
			throw new Exception("主键为空");
		}
		Payway ob = new Payway().id(id);
		try {
			ob = payMapper.queryOne(ob);
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return ob;
	}

	/**
	 * 获取全部有效的支付方式
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<Payway> getAll() throws Exception {
		List<Payway> obs = null;
		try {
			obs = payMapper.queryAll(new Payway());
		} catch (Exception e) {
			logger.error(e);
			throw new Exception("获取数据失败");
		}
		return obs;
	}
}
